<?php
class CPageCache extends CRecord
{
	public $all_blocks = array();
	public $path;
	public $module;
	public $region;
	public $start_id;
	function __construct($data, $module) {
		if($data) {
			$this->path = $data['path'];
			$this->module = $module;
			$this->start_id = 0;
			//$module?request('module_id',0):0;
			parent::__construct(CXml($data['path'])->decode());
			$this->all_blocks = array();
			$this->all_blocks($this->blocks, -1);
		} else {
			$this->path = '';
			$this->module = $module;
			$this->start_id = 0;
			parent::__construct(
				array(
					'name'=>'run_by_module',
					'path'=>'',
					'blocks' => array(
						array(
							'module'=>$module,
							'region' => 'center',
							'position' => 1,
						)
					)
				)
			);
			$this->all_blocks = array();
			$this->all_blocks($this->blocks, -1);
		}
	}

	function cache($cache_name) {
		return $this->component_cache().$this->layout_cache();
	}

	function all_blocks($blocks, $parent_id) {
		if($blocks and is_array($blocks)) {
			foreach($blocks as $order => &$block) {
				$block['index'] = $order;
			}

			// ????????????????????
			$blocks = unserialize(serialize(CList($blocks)->order('position')->items));
			foreach($blocks as $order => $block) {
				if (!$this->module or $block['module'] == $this->module or $parent_id != -1) {
					if($parent_id == -1) {
						$this->region = isset($block['region'])?$block['region']:'center';
					}
					$this->all_blocks[] = $block + array('id'=>$block_id = $this->start_id++, 'parent_id'=>$parent_id);
					if($sub_blocks = CTable('settings/blocks/block')->change_store(
						new CXmlStore(CModule::module_path($block['module']).'/module.xml'))->select_all(
					)) {
						$this->all_blocks($sub_blocks, $block_id);
					}
					if(isset($block['blocks']) and is_array($block['blocks'])) {
						$this->all_blocks($block['blocks'], $block_id);
					}
					if ($block['module'] == $this->module and $parent_id == -1)
						break;
				}
			}
		}
	}


	/**
	 * Enter description here...
	 *
	 * @return unknown
	 */
	function component_cache() {
		$block_codes = $this->export_blocks($module_records);
		//$this->data['blocks'] = $this->all_blocks;
		unset($this->data['blocks']);
		return '<?php'.$this->module_require($module_records).'
	CPage::$current = CPage('.var_export($this->data,true).');
	CPage::$current->all_blocks = array('.$block_codes.');
	CPage::$current->submit();
	?>';
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $module_records
	 * @return unknown
	 */
	function module_require($module_records) {
		$code = '';
		foreach($module_records as &$module_record) {
			//Khoanb Edit 28.08.09 Cache module Path
			  $code .= '
	require_once ROOT_DIR."'.$module_record['path'].'/class.php";
';
		}
		return $code;
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $module_records
	 * @param unknown_type $module_config
	 * @return unknown
	 */
	function export_blocks(&$module_records) {
		$module_records = array();
		$code = '';
		foreach($this->all_blocks as $block)
		{
			if(isset($block['blocks'])) {
				unset($block['blocks']);
			}
			if(isset(CPortal::$components['MODULE.'.$block['module']]) and $module = CPortal::$components['MODULE.'.$block['module']])
			{
				$module_records[$block['module']] = $module;
				$block_id = $block['id'];
				$parent_id = $block['parent_id'];
				if($parent_id == -1) {
					$parent_id -= CPage::$max_module_id;
				}
				unset($block['id']);
				unset($block['parent_id']);

				$config = var_export(array_merge(CXml($module['path'].'/module.xml')->decode() +
				  array('path'=>$module['path']),$block), true);
				$code .= '
	new '.$module['name'].'('.$config.'+array(
					\'id\' => CPage::$max_module_id + '.$block_id.',
					\'parent_id\' => '.(($parent_id==-1)?-1:'CPage::$max_module_id + '.$parent_id).',
				)),';
			} else {
				$code .= 'NULL,';
			}
		}
		return $code;
	}

	private function params($code) {
		return preg_replace('/\{(\w+)\}/', '{CPage::$current->params(\'$1\')}', $code);
	}
	function layout_cache()
	{
		
		if($this->module)
		{
			return '<?php region(\''.$this->region.'\');?>';
		}
		else
		{
			$replaces = array(
				'{header}' => CPage::$edit_mode?'':$this->params(file_get_contents('lib/portal/page_header.tpl')),
				'{footer}' => CPage::$edit_mode?'':$this->params(file_get_contents('lib/portal/page_footer.tpl')),
			);
			return CFormLayout(CFile::exists($this->layout)?
				strtr(file_get_contents($this->layout), $replaces):'')->parse();
		}
	}
}
?>